<?php

class EmailNotification
{
    public $emailTemplatesPath = '';
    
    public function __construct()
    {
        $this->emailTemplatesPath = Yii::getPathOfAlias('webroot').'/themes/'.Yii::app()->theme->getName().'/templates/email_templates/';
    }
    
    public function sendEmailToAdminAfterRegister($oUser, $oProfile, $oTariffPlan)
    {
        $aTariffPlans   = TariffPlan::getTariffPlans();
        $sDate = date('d-m-Y', $oUser->user_cdate);
        $sBody = Yii::app()->controller->renderFile($this->emailTemplatesPath.'email_to_admin_after_register.php', array
        (
            'oProfile' => $oProfile,
            'oTariffPlan' => $oTariffPlan,
            'sDate' => $sDate,
        ), true);
        $oMailMessage = new YiiMailMessage();
        $oMailMessage->subject = 'Запрос на регистрацию нового пользователя: '.$aTariffPlans[$oTariffPlan->tariff_plan].'. <Время регистрации – '.$sDate.'>.';
        $oMailMessage->setBody($sBody, 'text/html');
        $oMailMessage->addTo(Yii::app()->params['ifin_email']);
        $oMailMessage->from = 'noreply@ifin.by';
        Yii::app()->mail->send($oMailMessage);
    }

    public function sendEmailToAdminFromContactForm($oContact)
    {
        $sBody = Yii::app()->controller->renderFile($this->emailTemplatesPath.'email_to_admin_from_contact_form.php', array
        (
            'oContact' => $oContact
        ), true);
        $oMailMessage = new YiiMailMessage();
        $oMailMessage->subject = 'Новое сообщение с формы контактов';
        $oMailMessage->setBody($sBody, 'text/html');
        $oMailMessage->addTo(Yii::app()->params['ifin_email']);
        $oMailMessage->from = 'noreply@ifin.by';
        Yii::app()->mail->send($oMailMessage);
    }
    
    public function sendEmailToUserAfterRegister($oProfile, $oTariffPlan)
    {
        $sBody = Yii::app()->controller->renderFile($this->emailTemplatesPath.'email_to_user_after_register.php', array
        (
            'oProfile' => $oProfile,
            'oTariffPlan' => $oTariffPlan,
        ), true);

        $oMailMessage = new YiiMailMessage();
        $oMailMessage->subject = 'Добро пожаловать на iFin.by! Регистрация.';
        $oMailMessage->setBody($sBody, 'text/html');
        $oMailMessage->addTo($oProfile->email);
        $oMailMessage->from = 'noreply@ifin.by';
        Yii::app()->mail->send($oMailMessage);
    }
    
    public function sendEmailToUserSoleProprietorshipAfterRegister($oUser, $oProfile, $sHash, $sVerifyCode)
    {
        $sBody = Yii::app()->controller->renderFile($this->emailTemplatesPath.'email_to_user_sole_proprietorship_after_register.php', array
        (
            'oUser' => $oUser,
            'oProfile' => $oProfile,
            'sHash' => $sHash,
            'sVerifyCode' => $sVerifyCode,
        ), true);

        $oMailMessage = new YiiMailMessage();
        $oMailMessage->subject = 'Добро пожаловать на iFin.by! Регистрация.';
        $oMailMessage->setBody($sBody, 'text/html');
        $oMailMessage->addTo($oProfile->email);
        $oMailMessage->addBcc('ifinby@gmail.com');
        $oMailMessage->from = 'noreply@ifin.by';
        Yii::app()->mail->send($oMailMessage);
    }
    
    public function sendEmailToUserAfterRegisterApprove($oUser, $oTariffPlan, $sHash)
    {
        $sBody = Yii::app()->controller->renderFile($this->emailTemplatesPath.'email_to_user_after_register_approve.php', array
        (
            'oUser'       => $oUser,
            'oTariffPlan' => $oTariffPlan,
            'sHash'       => $sHash
        ), true);

        $oMailMessage = new YiiMailMessage();
        $oMailMessage->subject = 'Активация учетной записи на iFin.by успешно завершена.';
        $oMailMessage->setBody($sBody, 'text/html');
        $oMailMessage->addTo($oUser->profile->email);
        $oMailMessage->addBcc('ifinby@gmail.com');
        $oMailMessage->from = 'noreply@ifin.by';
        Yii::app()->mail->send($oMailMessage);
    }
    
    public function sendEmailToUserAfterRegisterDecline($oUser)
    {
        $sBody = Yii::app()->controller->renderFile($this->emailTemplatesPath.'email_to_user_after_register_decline.php', array
        (
            'oUser'       => $oUser
        ), true);

        $oMailMessage = new YiiMailMessage();
        $oMailMessage->subject = 'Регистрация на iFin.by отклонена.';
        $oMailMessage->setBody($sBody, 'text/html');
        $oMailMessage->addTo($oUser->profile->email);
        $oMailMessage->from = 'noreply@ifin.by';
        Yii::app()->mail->send($oMailMessage);
    }
    
    public function sendEmailToUserAfterApproveTariffPlan($oUser, $oTariffPlan)
    {
        $sBody = Yii::app()->controller->renderFile($this->emailTemplatesPath.'email_to_user_after_approve_tariff_plan.php', array
        (
            'oUser'       => $oUser,
            'oTariffPlan' => $oTariffPlan,
        ), true);

        $oMailMessage = new YiiMailMessage();
        $oMailMessage->subject = 'iFin.by. Тарифный план: '.$oTariffPlan->name.'. Заявка подтверждена.';
        $oMailMessage->setBody($sBody, 'text/html');
        $oMailMessage->addTo($oUser->profile->email);
        $oMailMessage->from = 'noreply@ifin.by';
        Yii::app()->mail->send($oMailMessage);
    }
    
    public function sendEmailToUserAfterDeclineTariffPlan($oUser)
    {
        $sBody = Yii::app()->controller->renderFile($this->emailTemplatesPath.'email_to_user_after_decline_tariff_plan.php', array
        (
            'oUser'       => $oProfile
        ), true);

        $oMailMessage = new YiiMailMessage();
        $oMailMessage->subject = 'iFin.by. Заявка на покупку дополнительного тарифного плана отклонена.';
        $oMailMessage->setBody($sBody, 'text/html');
        $oMailMessage->addTo($oUser->profile->email);
        $oMailMessage->from = 'noreply@ifin.by';
        Yii::app()->mail->send($oMailMessage);
     }
    
    public function sendEmailToUserAfterApproveAdditionalLot($oUser, $oTariffPlan)
    {
        $sBody = Yii::app()->controller->renderFile($this->emailTemplatesPath.'email_to_user_after_approve_additional_lot.php', array
        (
            'oUser'       => $oUser,
            'oTariffPlan' => $oTariffPlan
        ), true);

        $oMailMessage = new YiiMailMessage();
        $oMailMessage->subject = 'iFin.by. Запрос на покупку дополнительной заявки подтверждена.';
        $oMailMessage->setBody($sBody, 'text/html');
        $oMailMessage->addTo($oUser->profile->email);
        $oMailMessage->from = 'noreply@ifin.by';
        Yii::app()->mail->send($oMailMessage);
    }
    
    public function sendEmailToUserAfterDeclineAdditionalLot($oUser)
    {
        $sBody = Yii::app()->controller->renderFile($this->emailTemplatesPath.'email_to_user_after_decline_additional_lot.php', array
        (
            'oUser'       => $oProfile
        ), true);

        $oMailMessage = new YiiMailMessage();
        $oMailMessage->subject = 'iFin.by. Запрос на покупку дополнительной заявки отклонена.';
        $oMailMessage->setBody($sBody, 'text/html');
        $oMailMessage->addTo($oUser->profile->email);
        $oMailMessage->from = 'noreply@ifin.by';
        Yii::app()->mail->send($oMailMessage);
    }

    public function sendEmailToUserCreateBid($oUser, $oBid, $oLot)
    {
		$oClient = User::model()->findByPk($oBid->user_id);
		
        $sBody = Yii::app()->controller->renderFile($this->emailTemplatesPath.'email_to_user_create_bid.php', array
        (
            'oBid' => $oBid,
            'oLot' => $oLot,
			'oClient' => $oClient
        ), true);

        $oMailMessage = new YiiMailMessage();
        $oMailMessage->subject = 'iFin.by. Предложение по заявке #'.$oLot->lot_number.'.';
        $oMailMessage->setBody($sBody, 'text/html');
        $oMailMessage->addTo($oUser->profile->email);
        $oMailMessage->from = 'noreply@ifin.by';
        Yii::app()->mail->send($oMailMessage);
    }
    
    public function sendEmailToUserBeginTalks($oUser, $oBid, $oLot)
    {
		$oClient = User::model()->findByPk($oLot->user_id);
		
        $sBody = Yii::app()->controller->renderFile($this->emailTemplatesPath.'email_to_user_begin_talks.php', array
        (
            'oLot' => $oLot,
            'oBid' => $oBid,
            'oClient' => $oClient
        ), true);

        $oMailMessage = new YiiMailMessage();
        $oMailMessage->subject = 'iFin.by. Предложение переговоров по Вашей заявке #'.$oLot->lot_number.'.';
        $oMailMessage->setBody($sBody, 'text/html');
        $oMailMessage->addTo($oUser->profile->email);
        $oMailMessage->from = 'noreply@ifin.by';
        Yii::app()->mail->send($oMailMessage);
    }

    public function sendEmailToUserResetPassword($oUser, $sNewPassword)
    {
        $sBody = Yii::app()->controller->renderFile($this->emailTemplatesPath.'email_to_user_reset_password.php', array
        (
            'oUser' => $oUser,
            'sNewPassword' => $sNewPassword
        ), true);

        $oMailMessage = new YiiMailMessage();
        $oMailMessage->subject = ' iFin.by новый пароль.';
        $oMailMessage->setBody($sBody, 'text/html');
        $oMailMessage->addTo($oUser->profile->email);
        $oMailMessage->addBcc('ifinby@gmail.com');
        $oMailMessage->from = 'noreply@ifin.by';
        Yii::app()->mail->send($oMailMessage);
    }

}